﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoccerScore.Compact.Linq;

namespace Soccer_Score_Forecast.BLL_CurveFit
{
    public static class dMatch
    {
        public static ILookup<int?, Result_tb_lib> lookupResultHome { get; set; }
        public static ILookup<int?, Result_tb_lib> lookupResultAway { get; set; }
        public static ILookup<string, MacauPredication> lookupMacau { get; set; }
        public static ILookup<int, live_Table_lib> lookupLive { get; set; }
        //public static ILookup<int, Result_tb_lib> lookupResultID = null;
        //public static bool lookupNewEnable = false;
        private static MatchDataCache mdCache;
        public static bool LoadMatchData(bool reload)
        {
            if (reload == true)
            {
                Disponse();
                mdCache = new MatchDataCache();
                lookupResultHome = mdCache.lookupResultHome;
                lookupResultAway = mdCache.lookupResultAway;
                lookupMacau = mdCache.lookupMacau;
                lookupLive = mdCache.lookupLive;
                //lookupNewEnable = true;
                GC.Collect();
            }
            return reload;
        }
        public static void Disponse()
        {
            if (mdCache != null)
                mdCache.Dispose();
            lookupResultHome = null;
            lookupResultAway = null;
            lookupMacau = null;
            lookupLive = null;
            GC.Collect();
        }
    }

    public class MatchDataCache : IDisposable
    {
        public ILookup<int?, Result_tb_lib> lookupResultHome { get; set; }
        public ILookup<int?, Result_tb_lib> lookupResultAway { get; set; }
        public ILookup<string, MacauPredication> lookupMacau { get; set; }
        public ILookup<int, live_Table_lib> lookupLive { get; set; }
        //public ILookup<int, Result_tb_lib> lookupResultID { get; set; }
        public MatchDataCache()
        {
            using (DataClassesMatchDataContext matches = new DataClassesMatchDataContext(Conn.conn))
            {
                lookupResultHome = matches.Result_tb_lib.AsParallel().ToLookup(e => e.Home_team_big);
                lookupResultAway = matches.Result_tb_lib.AsParallel().ToLookup(e => e.Away_team_big);
                //lookupResultID = matches.Result_tb_lib.ToLookup(e => e.Result_tb_lib_id);
                lookupMacau = matches.MacauPredication.AsParallel().ToLookup(e => e.Home_team + "-" + e.Away_team);
                lookupLive = matches.live_Table_lib.AsParallel().ToLookup(e => e.live_table_lib_id);
            }
        }
        #region Implementing IDisposable and the Dispose Pattern Properly
        private bool disposed = false; // to detect redundant calls
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        ~MatchDataCache()
        {
            Dispose(false);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing) { }
                disposed = true;
            }
        }
        #endregion
    }
}
